﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;

namespace app.Api
{
    /*
        数据库的初始化，备份等操作

        初始化的目的是保证一些必要的数据添加到数据库中，甚至可以自动建立表（通过执行sql文件代码）等
    */
    public class ApiCheck : ApiBase
    {
        public ApiCheck(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        // 作者
        string req_author { get { return GetStringParam(new ReqStringParamOption { Name = "author", Level = REQPARAMLEVEL.FREE }); } }

        #endregion

        public void Do(string action)
        {
            Member member = null;
            ServiceItem service = null;
            DemandItem demand = null;
            switch (action)
            {
                case "investment_pass":
                    {
                        ServiceItem investment = G.DB.GetById<ServiceItem>(req_id);
                        investment.status = (int)SERVICE_STATUS.POSTED;
                        G.DB.Update<ServiceItem>(investment);
                        ResponseJson(new Result());
                        break;
                    }
                case "investment_failed":
                    {
                        ServiceItem investment = G.DB.GetById<ServiceItem>(req_id);
                        investment.status = (int)SERVICE_STATUS.FAILED_EXAMINE;
                        G.DB.Update<ServiceItem>(investment);
                        ResponseJson(new Result());
                        break;
                    }
                case "investment_list":
                    {
                        ResponseJson(GetPageResult<ServiceItem>(string.Format("status ={0}", (int)SERVICE_STATUS.EXAMINING), G.DB.GetPrimaryKey<ServiceItem>()));
                        break;
                    }
                //获取待审核的企业服务机构列表
                case "demands":
                    {
                        string enterprise_name = GetStringParam(new ReqStringParamOption { Name = "enterprise_name", Level = REQPARAMLEVEL.FREE });
                        string number = GetStringParam(new ReqStringParamOption { Name = "number", Level = REQPARAMLEVEL.FREE });
                        Cd cd = new Cd();
                        cd.And("role_type = 4 or role_type = 3");
                        cd.And("status = 1");
                        if (enterprise_name != "")
                        {
                            cd.And("enterprise_name like '%{0}%'", enterprise_name);
                        }
                        if (number != "")
                        {
                            cd.And("number like '%{0}%'", number);
                        }
                        Ob ob = new Ob();
                        ob.Desc("create_time");
                        ResponseJson(GetPageResult<Member>(cd, ob));
                        //ResponseJson(GetPageResult<Member>(" role_type = 4 and status = 1", G.DB.GetPrimaryKey<Member>()));//or role_type = 4 获取待审核的企业
                        break;
                    }
                case "services":
                    {
                        string enterprise_name = GetStringParam(new ReqStringParamOption { Name = "enterprise_name", Level = REQPARAMLEVEL.FREE });
                        string number = GetStringParam(new ReqStringParamOption { Name = "number", Level = REQPARAMLEVEL.FREE });
                        Cd cd = new Cd();
                        cd.And("role_type = 5");
                        cd.And("status = 1");
                        if (enterprise_name != "")
                        {
                            cd.And("enterprise_name like '%{0}%'", enterprise_name);
                        }
                        if (number != "")
                        {
                            cd.And("number like '%{0}%'", number);
                        }
                        Ob ob = new Ob();
                        ob.Desc("create_time");
                        ResponseJson(GetPageResult<Member>(cd, ob));//获取待审核的服务机构 role_type = 5
                        break;
                    }
                //企业服务机构审核
                case "fs_check_pass":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.FIRM_SERVICE_CHECK).Check());
                        member = G.DB.GetById<Member>(req_id);
                        if (member.status == 1)
                        {
                            member.status = 2;//审核成功  Convert.ToInt32(AUTH_STATUS.PASS)
                            if (G.DB.Update<Member>(member) == 0)
                            {
                                ThrowStatus(STATUS.FAILED_UPDATE, "审核失败！");
                            }
                            else
                            {
                                SendMessage(member.id, string.Format("【{0}】审核成功。", member.enterprise_name), string.Format("恭喜您完善的【{0}】基本信息审核成功", member.enterprise_name));
                                ResponseJson(new Result());
                                RecordLog(PERMISSIONOPERATION.CHECK, UserName + "审核通过：" + member.enterprise_name);
                            }
                        }
                        else
                        {
                            ThrowStatus(STATUS.FAILED_UPDATE, "*其他管理员已审核改记录！");
                        }
                        break;
                    }
                case "fs_check_fail":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.FIRM_SERVICE_CHECK).Check());
                        member = G.DB.GetById<Member>(req_id);
                        string reason = GetStringParam(new ReqStringParamOption { Name = "reason" });
                        if (member.status == 1)
                        {
                            member.status = 3;//审核未通过
                            if (G.DB.Update<Member>(member) == 0)
                            {
                                ThrowStatus(STATUS.FAILED_UPDATE, "审核失败！");
                            }
                            else
                            {
                                SendMessage(member.id, string.Format("【{0}】审核未通过。", member.enterprise_name), reason);
                                ResponseJson(new Result());
                                RecordLog(PERMISSIONOPERATION.CHECK, UserName + "审核未通过：" + member.enterprise_name);
                            }
                        }
                        else
                        {
                            ThrowStatus(STATUS.FAILED_UPDATE, "*其他管理员已审核该记录！");
                        }
                        break;
                    }
                //获取待审核服务列表
                case "service":
                    {
                        string name = GetStringParam(new ReqStringParamOption { Name = "name", Level = REQPARAMLEVEL.FREE });
                        string number = GetStringParam(new ReqStringParamOption { Name = "number", Level = REQPARAMLEVEL.FREE });
                        Cd cd = new Cd();
                        Ob ob = new Ob();
                        cd.And("status = 1");
                        if (name != "")
                        {
                            cd.And("title like '%{0}%'", name);
                        }
                        if (number != "")
                        {
                            cd.And("number like '%{0}%'", number);
                        }
                        ob.Desc("create_time");
                        ResponseJson(GetPageResult<ServiceItem>(cd, ob));
                        break;
                    }
                //服务审核
                case "service_check":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.SERVICE_CHECK).Check());
                        service = G.DB.GetById<ServiceItem>(req_id);
                        if (service.status == 2 || service.status == 3) ThrowStatus(STATUS.FAILED_UPDATE, "*其他管理员已审核该记录！");
                        if (req_status == 1) service.status = 2;//审核成功
                        else service.status = 3;
                        if (G.DB.Update<ServiceItem>(service) == 0)
                        {
                            ThrowStatus(STATUS.FAILED_UPDATE, "审核失败！");
                        }
                        else
                        {
                            if (service.status == 2)
                            {
                                SendMessage(service.member.id, string.Format("【{0}】审核成功。", service.title), string.Format("恭喜您发布的服务【{0}】审核成功!", service.title));
                                RecordLog(PERMISSIONOPERATION.CHECK, UserName + "审核通过名为：" + service.title + "的服务");
                            }
                            else
                            {
                                string reason = GetStringParam(new ReqStringParamOption { Name = "reason" });
                                SendMessage(service.member.id, string.Format("【{0}】审核未通过。", service.title), reason);
                                RecordLog(PERMISSIONOPERATION.CHECK, UserName + "审核未通过名为：" + service.title + "的服务");
                            }
                            ResponseJson(new Result());
                        }
                        break;
                    }
                //获得所有待审核的需求
                case "demand":
                    {
                        string name = GetStringParam(new ReqStringParamOption { Name = "name", Level = REQPARAMLEVEL.FREE });
                        string number = GetStringParam(new ReqStringParamOption { Name = "number", Level = REQPARAMLEVEL.FREE });
                        Cd cd = new Cd();
                        Ob ob = new Ob();
                        cd.And("status = 1");
                        if (name != "")
                        {
                            cd.And("title like '%{0}%'", name);
                        }
                        if (number != "")
                        {
                            cd.And("number like '%{0}%'", number);
                        }
                        ob.Desc("create_time");
                        ResponseJson(GetPageResult<DemandItem>(cd, ob));
                        break;
                    }
                //需求审核通过
                case "demand_pass":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.DEMAND_CHECK).Check());
                        demand = G.DB.GetById<DemandItem>(req_id);
                        if (demand.status == 1)
                        {
                            demand.status = 2;//成功
                            if (G.DB.Update<DemandItem>(demand) == 0)
                            {
                                ThrowStatus(STATUS.FAILED_UPDATE, "*审核失败！");
                            }
                            else
                            {
                                SendMessage(demand.member.id, string.Format("【{0}】审核成功。", demand.title), string.Format("恭喜您发布的【{0}】审核成功", demand.title));
                                ResponseJson(new Result());
                                RecordLog(PERMISSIONOPERATION.CHECK, UserName + "审核通过名为：" + demand.title + "的需求");
                            }
                        }
                        else
                        {
                            ThrowStatus(STATUS.FAILED_UPDATE, "*其他管理员已审核改记录！");
                        }
                        break;
                    }
                //需求审核未通过
                case "demand_fail":
                    {
                        CheckPermission(IDENTITY.ADMIN, Permission.Module(PERMISSIONMODULE.DEMAND_CHECK).Check());
                        demand = G.DB.GetById<DemandItem>(req_id);
                        string reason = GetStringParam(new ReqStringParamOption { Name = "reason" });
                        if (demand.status == 1)
                        {
                            demand.status = 3;//失败
                            if (G.DB.Update<DemandItem>(demand) == 0)
                            {
                                ThrowStatus(STATUS.FAILED_UPDATE, "审核失败！");
                            }
                            else
                            {
                                SendMessage(demand.member.id, string.Format("【{0}】审核未通过。", demand.title), reason);
                                ResponseJson(new Result());
                                RecordLog(PERMISSIONOPERATION.CHECK, UserName + "审核未通过名为：" + demand.title + "的需求");
                            }
                        }
                        else
                        {
                            ThrowStatus(STATUS.FAILED_UPDATE, "*其他管理员已审核改记录！");
                        }
                        break;
                    }
                default: break;
            }
        }
    }


}